Conditions | 1 |
Paths | 368 |
Total Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(['helper'], function (helper) { |
||
2 | var self = {}; |
||
3 | |||
4 | var ctx; |
||
5 | var width; |
||
6 | var height; |
||
7 | |||
8 | var transform; |
||
9 | |||
10 | var highlight; |
||
11 | |||
12 | var nodeColor = '#fff'; |
||
13 | var clientColor = '#e6324b'; |
||
14 | |||
15 | var cableColor = '#50b0f0'; |
||
16 | var highlightColor = 'rgba(255, 255, 255, 0.2)'; |
||
17 | |||
18 | var labelColor = '#fff'; |
||
19 | |||
20 | var NODE_RADIUS = 15; |
||
21 | var LINE_RADIUS = 12; |
||
22 | |||
23 | function drawDetailNode(d) { |
||
24 | if (transform.k > 1) { |
||
25 | ctx.beginPath(); |
||
26 | helper.positionClients(ctx, d, Math.PI, d.o.node.statistics.clients, 15); |
||
27 | ctx.fillStyle = clientColor; |
||
28 | ctx.fill(); |
||
29 | ctx.beginPath(); |
||
30 | var name = d.o.node_id; |
||
31 | if (d.o.node && d.o.node.nodeinfo) { |
||
32 | name = d.o.node.nodeinfo.hostname; |
||
33 | } |
||
34 | ctx.textAlign = 'center'; |
||
35 | ctx.fillStyle = labelColor; |
||
36 | ctx.fillText(name, d.x, d.y + 20); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | function drawHighlightNode(d) { |
||
41 | if (highlight && highlight.type === 'node' && d.o.node === highlight.o) { |
||
42 | ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI); |
||
43 | ctx.fillStyle = highlightColor; |
||
44 | ctx.fill(); |
||
45 | ctx.beginPath(); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | function drawHighlightLink(d, to) { |
||
50 | if (highlight && highlight.type === 'link' && d.o === highlight.o) { |
||
51 | ctx.lineTo(to[0], to[1]); |
||
52 | ctx.strokeStyle = highlightColor; |
||
53 | ctx.lineWidth = LINE_RADIUS * 2; |
||
54 | ctx.lineCap = 'round'; |
||
55 | ctx.stroke(); |
||
56 | to = [d.source.x, d.source.y]; |
||
57 | } |
||
58 | return to; |
||
59 | } |
||
60 | |||
61 | self.drawNode = function drawNode(d) { |
||
62 | if (d.x < transform.invertX(0) || d.y < transform.invertY(0) || transform.invertX(width) < d.x || transform.invertY(height) < d.y) { |
||
63 | return; |
||
64 | } |
||
65 | ctx.beginPath(); |
||
66 | |||
67 | drawHighlightNode(d); |
||
68 | |||
69 | ctx.moveTo(d.x + 3, d.y); |
||
70 | ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI); |
||
71 | ctx.fillStyle = nodeColor; |
||
72 | ctx.fill(); |
||
73 | |||
74 | drawDetailNode(d); |
||
75 | }; |
||
76 | |||
77 | self.drawLink = function drawLink(d) { |
||
78 | var zero = transform.invert([0, 0]); |
||
79 | var area = transform.invert([width, height]); |
||
80 | if (d.source.x < zero[0] && d.target.x < zero[0] || d.source.y < zero[1] && d.target.y < zero[1] || |
||
81 | d.source.x > area[0] && d.target.x > area[0] || d.source.y > area[1] && d.target.y > area[1]) { |
||
82 | return; |
||
83 | } |
||
84 | ctx.beginPath(); |
||
85 | ctx.moveTo(d.source.x, d.source.y); |
||
86 | var to = [d.target.x, d.target.y]; |
||
87 | |||
88 | to = drawHighlightLink(d, to); |
||
89 | |||
90 | ctx.lineTo(to[0], to[1]); |
||
91 | ctx.strokeStyle = d.o.type === 'Kabel' ? cableColor : d.color; |
||
92 | if (d.o.type === 'fastd' || d.o.type === 'L2TP') { |
||
93 | ctx.globalAlpha = 0.2; |
||
94 | ctx.lineWidth = 1.5; |
||
95 | } else { |
||
96 | ctx.globalAlpha = 0.8; |
||
97 | ctx.lineWidth = 2.5; |
||
98 | } |
||
99 | ctx.stroke(); |
||
100 | }; |
||
101 | |||
102 | self.setCTX = function setCTX(newValue) { |
||
103 | ctx = newValue; |
||
104 | }; |
||
105 | |||
106 | self.setHighlight = function setHighlight(newValue) { |
||
107 | highlight = newValue; |
||
108 | }; |
||
109 | |||
110 | self.setTransform = function setTransform(newValue) { |
||
111 | transform = newValue; |
||
112 | }; |
||
113 | |||
114 | self.setMaxArea = function setMaxArea(newWidth, newHeight) { |
||
115 | width = newWidth; |
||
116 | height = newHeight; |
||
117 | }; |
||
118 | |||
119 | return self; |
||
120 | }); |
||
121 |